package cn.dw.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.dw.model.Reader;

public class AddStudentUntil {
	
	public void addStudent(Reader student){
		
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		
		HttpPost httpPost =new HttpPost("http://10.20.2.9/cgi-bin/HNModify.cgi");
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("type", "4"));//操作类型新增人员 
		params.add(new BasicNameValuePair("id", ""));//此操作类型下无意义
		params.add(new BasicNameValuePair("eid", student.getDzzh()));//员工号
		params.add(new BasicNameValuePair("name", student.getDzxm()));//人员姓名
		params.add(new BasicNameValuePair("gender", "0"));//人员性别
		params.add(new BasicNameValuePair("parentId ", "3"));//人员所属部门ID
		params.add(new BasicNameValuePair("picture", ""));//照片
		params.add(new BasicNameValuePair("validity", "2099-10-10"));//有效期
		params.add(new BasicNameValuePair("synVersion", "0"));//固定为0
		System.out.println(student.getDzzh());
		System.out.println(student.getDzxm());
		
		try{
		System.out.println("开始执行");
		httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpPost);
		String strResult= EntityUtils.toString(response.getEntity(),"utf-8");
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		System.out.println("结束");
		
		try {
			CloseableHttpResponse response2 = httpclient.execute(httpPost);
		    System.out.println(response2.getStatusLine()+"-------------");
		    HttpEntity entity2 = response2.getEntity();
		    EntityUtils.consume(entity2);
		    response2.close();
		}catch (Exception e) {
			System.out.println("接受返回值失败！");
		}
	}
	
	public void updateStudent(Reader student){
		
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		
		HttpPost httpPost =new HttpPost("http://10.20.2.9/cgi-bin/HNModify.cgi");
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("type", "1"));//操作类型新增人员 
		params.add(new BasicNameValuePair("id", ""));//此操作类型下无意义
		params.add(new BasicNameValuePair("eid", student.getDzzh()));//员工号
		params.add(new BasicNameValuePair("name", student.getDzxm()));//人员姓名
		params.add(new BasicNameValuePair("gender", "0"));//人员性别
		params.add(new BasicNameValuePair("parentId ", "3"));//人员所属部门ID
		params.add(new BasicNameValuePair("picture", ""));//照片
		params.add(new BasicNameValuePair("validity", "2099-10-10"));//有效期
		params.add(new BasicNameValuePair("synVersion", "0"));//固定为0
		
		try{
		System.out.println("开始执行");
		httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpPost);
		String strResult= EntityUtils.toString(response.getEntity(),"utf-8");
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		System.out.println("结束");
		
		try {
			CloseableHttpResponse response2 = httpclient.execute(httpPost);
		    System.out.println(response2.getStatusLine()+"-------------");
		    HttpEntity entity2 = response2.getEntity();
		    EntityUtils.consume(entity2);
		    response2.close();
		}catch (Exception e) {
			System.out.println("接受返回值失败！");
		}
	}
}
